﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAllAthleticPerformanceOfStudent : System.Web.UI.Page
    {
        string queryString;
        StudentAL studentAL;
        SportsAL sportsAL;
        AthleticAL athleticAL;
        Guid studentId;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 5;
        AdminAL adminAL;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                studentId = Guid.Empty;
                bool convertableToBool = Guid.TryParse(queryString, out studentId);
                if (convertableToBool)
                {
                    BindData();
                }
                else
                {
                    return;
                }

            }
        }



        private void BindData()
        {

            studentAL = new StudentAL();
            sportsAL = new SportsAL();
            athleticAL = new AthleticAL();
            List<AthleticDataDTO> allAthleticDataOfStudent = athleticAL.GetAllAthleticDataOfStudent(studentId).ToList();
            List<AthleticDataDTO> allAthleticDataOfStudentOfOnePage = new List<AthleticDataDTO>();
            //gvViewAllMembers.DataSource = allMembers;
            //gvViewAllMembers.DataBind();


            // get the total rows 
            double totalRows = (int)allAthleticDataOfStudent.Count;

            lblAllAthleticOfStudentTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllAthleticOfStudentCurrPage.Text = currentPageNumber.ToString();

            lblAllAthleticOfStudentPage.Text = "Displaying all of the excellence of a student in all athletics over the period  - Page ";
            lblAllAthleticOfStudentOf.Text = "  of  ";

            int totalPageNumber = lblAllAthleticOfStudentTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllAthleticOfStudentTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllAthleticOfStudentFirst.Enabled = false;
                    btnAllAthleticOfStudentPrev.Enabled = false;
                    btnAllAthleticOfStudentNext.Enabled = false;
                    btnAllAthleticOfStudentLast.Enabled = false;
                    allAthleticDataOfStudentOfOnePage = allAthleticDataOfStudent;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllAthleticOfStudentFirst.Enabled = false;
                    btnAllAthleticOfStudentPrev.Enabled = false;
                    btnAllAthleticOfStudentNext.Enabled = true;
                    btnAllAthleticOfStudentLast.Enabled = true;
                    allAthleticDataOfStudentOfOnePage = allAthleticDataOfStudent.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllAthleticOfStudentFirst.Enabled = true;
                    btnAllAthleticOfStudentPrev.Enabled = true;
                    btnAllAthleticOfStudentNext.Enabled = true;
                    btnAllAthleticOfStudentLast.Enabled = true;
                    allAthleticDataOfStudentOfOnePage = allAthleticDataOfStudent.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllAthleticOfStudentFirst.Enabled = true;
                    btnAllAthleticOfStudentPrev.Enabled = true;
                    btnAllAthleticOfStudentNext.Enabled = false;
                    btnAllAthleticOfStudentLast.Enabled = false;
                    allAthleticDataOfStudentOfOnePage = allAthleticDataOfStudent.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allAthleticDataOfStudent.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }
            

            CreateDynamicTable(allAthleticDataOfStudentOfOnePage);

        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllAthleticOfStudentCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllAthleticOfStudentCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllAthleticOfStudentTotalPage.Text);
                    break;
            }


            BindData();
        }


        private void CreateDynamicTable(List<AthleticDataDTO> allAthleticDataOfStudentOfOnePage)
        {
            PlaceHolder4.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allAthleticDataOfStudentOfOnePage.Count;

            List<StudentDTO> studentDTO = new List<StudentDTO>();
            List<AthleticDataDTO> athleticDataDTO = new List<AthleticDataDTO>();
            List<AthleticEventDTO> athleticEventDTO = new List<AthleticEventDTO>();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);

            TableRow trHeader = new TableRow();
            trHeader.Width = new Unit("25%");

            HyperLink hlAthleticCellHeaderYr = new HyperLink();
            hlAthleticCellHeaderYr.Text = "Year";

            HyperLink hlAthleticCellHeaderLevel = new HyperLink();
            hlAthleticCellHeaderLevel.Text = "Level";

            HyperLink hlAthleticHeaderEvent = new HyperLink();
            hlAthleticHeaderEvent.Text = "Athletic Event";


            HyperLink hlAthleticCellHeaderDivision = new HyperLink();
            hlAthleticCellHeaderDivision.Text = "Age Division";



            HyperLink hlAthleticCellHeaderPlace = new HyperLink();
            hlAthleticCellHeaderPlace.Text = "Place";

            List<HyperLink> allHeaderHyperlinks = new List<HyperLink>();

            //based  on selected type change column orders


            allHeaderHyperlinks.Add(hlAthleticCellHeaderYr);
            allHeaderHyperlinks.Add(hlAthleticCellHeaderLevel);
            allHeaderHyperlinks.Add(hlAthleticHeaderEvent);
            allHeaderHyperlinks.Add(hlAthleticCellHeaderDivision);
            allHeaderHyperlinks.Add(hlAthleticCellHeaderPlace);



            foreach (HyperLink hyperlinkHeader in allHeaderHyperlinks)
            {
                TableCell tcHeader = new TableCell();
                tcHeader.BackColor = System.Drawing.Color.DarkSlateBlue;
                tcHeader.ForeColor = System.Drawing.Color.White;

                tcHeader.Controls.Add(hyperlinkHeader);
                trHeader.Cells.Add(tcHeader);



            }

            tbl.Rows.Add(trHeader);


            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");

                athleticEventDTO = athleticAL.GetAthleticEventById(allAthleticDataOfStudentOfOnePage[i].AthleticEventId).ToList();
                studentDTO = studentAL.GetStudentById(allAthleticDataOfStudentOfOnePage[i].StudentId).ToList();

                HyperLink hlAthleticCellYr = new HyperLink();
                hlAthleticCellYr.Text = allAthleticDataOfStudentOfOnePage[i].Year.ToString();

                HyperLink hlAthleticCellLevel = new HyperLink();
                hlAthleticCellLevel.Text = allAthleticDataOfStudentOfOnePage[i].LevelName.ToString();

                HyperLink hlAthleticCellEvent = new HyperLink();
                hlAthleticCellEvent.Text = athleticEventDTO[0].EventCategory.ToString();

                HyperLink hlAthleticCellDivision = new HyperLink();
                hlAthleticCellDivision.Text = athleticEventDTO[0].AgeDivision;

                HyperLink hlAthleticCellPlace = new HyperLink();
                hlAthleticCellPlace.Text = allAthleticDataOfStudentOfOnePage[i].Place;

                queryString = Request.QueryString.ToString();



                hlAthleticOfStudent.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;
                hlAthleticOfStudent.Text = studentDTO[0].FullName;

                lblAthleticOfStudent1.Text = "The excellence of ";
                lblAthleticOfStudent.Text = " in all athletics through out his college period ";

                //if (studentDTO.Count != 0)
                //{
                //    hlParticpatnOfStudent.Text = studentDTO[0].FullName;
                //    hlParticpatnOfStudent.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;
                //}
                //if (staffDTO.Count != 0)
                //{
                //    hlParticpatnOfStudent.Text = staffDTO[0].FullName;
                //    //this should be redirected to staff page
                //   // hlParticpatnOfStudent.NavigateUrl = "viewStudentById.aspx?" + staffDTO[0].StaffId;
                //}



                List<HyperLink> allHyperlinks = new List<HyperLink>();
                allHyperlinks.Add(hlAthleticCellYr);
                allHyperlinks.Add(hlAthleticCellLevel);
                allHyperlinks.Add(hlAthleticCellEvent);
                allHyperlinks.Add(hlAthleticCellDivision);
                allHyperlinks.Add(hlAthleticCellPlace);




                foreach (HyperLink hyperlink in allHyperlinks)
                {
                    TableCell tc = new TableCell();



                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                        tc.BackColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                        tc.BackColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hyperlink);


                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table

                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}